home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection Student Program / ADC Tools Sampler CD Disk 3 1999.iso / Metrowerks CodeWarrior / Java Support / Java_Source / Java2 / src / javax / swing / JToolTip.java < prev    next >
Encoding:
Java Source  |  1999-05-28  |  6.6 KB  |  234 lines  |  [TEXT/CWIE]

  1. /*
  2.  * %W% %E%
  3.  *
  4.  * Copyright 1997, 1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15.  
  16. package javax.swing;
  17. import javax.swing.plaf.*;
  18. import javax.accessibility.*;
  19.  
  20. import java.io.ObjectOutputStream;
  21. import java.io.ObjectInputStream;
  22. import java.io.IOException;
  23.  
  24.  
  25. /**
  26.  * Used to display a "Tip" for a Component. Typically components provide api
  27.  * to automate the process of using ToolTips. For example, any Swing component
  28.  * can use the JComponent <code>setToolTipText</code> method to specify the text
  29.  * for a standard tooltip. A component that wants to create a custom ToolTip
  30.  * display can override JComponent's <code>createToolTip</code> method and use
  31.  * a subclass of this class.
  32.  * <p>
  33.  * For the keyboard keys used by this component in the standard Look and
  34.  * Feel (L&F) renditions, see the
  35.  * <a href="doc-files/Key-Index.html#JToolTip">JToolTip</a> key assignments.
  36.  * <p>
  37.  * See <a href="http://java.sun.com/docs/books/tutorial/ui/swing/tooltip.html">How to Use Tool Tips</a>
  38.  * in <a href="http://java.sun.com/Series/Tutorial/index.html"><em>The Java Tutorial</em></a>
  39.  * for further documentation.
  40.  * <p>
  41.  * <strong>Warning:</strong>
  42.  * Serialized objects of this class will not be compatible with
  43.  * future Swing releases.  The current serialization support is appropriate
  44.  * for short term storage or RMI between applications running the same
  45.  * version of Swing.  A future release of Swing will provide support for
  46.  * long term persistence.
  47.  *
  48.  * @see JComponent#setToolTipText
  49.  * @see JComponent#createToolTip
  50.  * @version %I% %G%
  51.  * @author Dave Moore
  52.  * @author Rich Shiavi
  53.  */
  54. public class JToolTip extends JComponent implements Accessible {
  55.     /**
  56.      * @see #getUIClassID
  57.      * @see #readObject
  58.      */
  59.     private static final String uiClassID = "ToolTipUI";
  60.  
  61.     String tipText;
  62.     JComponent component;
  63.  
  64.     /** Creates a tool tip. */
  65.     public JToolTip() {
  66.         updateUI();
  67.     }
  68.  
  69.     /**
  70.      * Returns the L&F object that renders this component.
  71.      *
  72.      * @return the ToolTipUI object that renders this component
  73.      */
  74.     public ToolTipUI getUI() {
  75.         return (ToolTipUI)ui;
  76.     }
  77.  
  78.     /**
  79.      * Notification from the UIFactory that the L&F has changed. 
  80.      * Called to replace the UI with the latest version from the 
  81.      * UIFactory.
  82.      *
  83.      * @see JComponent#updateUI
  84.      */
  85.     public void updateUI() {
  86.         setUI((ToolTipUI)UIManager.getUI(this));
  87.     }
  88.  
  89.  
  90.     /**
  91.      * Returns the name of the L&F class that renders this component.
  92.      *
  93.      * @return "ToolTipUI"
  94.      * @see JComponent#getUIClassID
  95.      * @see UIDefaults#getUI
  96.      */
  97.     public String getUIClassID() {
  98.         return uiClassID;
  99.     }
  100.  
  101.  
  102.     /**
  103.      * Sets the text to show when the tool tip is displayed.
  104.      *
  105.      * @param tipText the String to display
  106.      */
  107.     public void setTipText(String tipText) {
  108.         this.tipText = tipText;
  109.     }
  110.  
  111.     /**
  112.      * Returns the text that is shown when the tool tip is displayed.
  113.      *
  114.      * @return the String that is displayed
  115.      */
  116.     public String getTipText() {
  117.         return tipText;
  118.     }
  119.  
  120.     /**
  121.      * Specifies the component that the tooltip describes.
  122.      *
  123.      * @see JComponent#createToolTip
  124.      */
  125.     public void setComponent(JComponent c) {
  126.         component = c;
  127.     }
  128.  
  129.     /**
  130.      * Returns the component the tooltip applies to.
  131.      * @return the component that the tooltip describes
  132.      *
  133.      * @see JComponent#createToolTip
  134.      */
  135.     public JComponent getComponent() {
  136.         return component;
  137.     }
  138.  
  139.     /**
  140.      * Always return true since tooltips, by definition, 
  141.      * should always be on top of all other windows.
  142.      */
  143.     // package private
  144.     boolean alwaysOnTop() {
  145.     return true;
  146.     }
  147.  
  148.  
  149.     /** 
  150.      * See readObject() and writeObject() in JComponent for more 
  151.      * information about serialization in Swing.
  152.      */
  153.     private void writeObject(ObjectOutputStream s) throws IOException {
  154.         s.defaultWriteObject();
  155.     if ((ui != null) && (getUIClassID().equals(uiClassID))) {
  156.         ui.installUI(this);
  157.     }
  158.     }
  159.  
  160.  
  161.     /**
  162.      * Returns a string representation of this JToolTip. This method 
  163.      * is intended to be used only for debugging purposes, and the 
  164.      * content and format of the returned string may vary between      
  165.      * implementations. The returned string may be empty but may not 
  166.      * be <code>null</code>.
  167.      * <P>
  168.      * Overriding paramString() to provide information about the
  169.      * specific new aspects of the JFC components.
  170.      * 
  171.      * @return  a string representation of this JToolTip.
  172.      */
  173.     protected String paramString() {
  174.         String tipTextString = (tipText != null ?
  175.                 tipText : "");
  176.  
  177.         return super.paramString() +
  178.         ",tipText=" + tipTextString;
  179.     }
  180.  
  181.  
  182. /////////////////
  183. // Accessibility support
  184. ////////////////
  185.  
  186.     /**
  187.      * Get the AccessibleContext associated with this JComponent
  188.      *
  189.      * @return the AccessibleContext of this JComponent
  190.      */
  191.     public AccessibleContext getAccessibleContext() {
  192.         if (accessibleContext == null) {
  193.             accessibleContext = new AccessibleJToolTip();
  194.         }
  195.         return accessibleContext;
  196.     }
  197.  
  198.     /**
  199.      * The class used to obtain the accessible role for this object.
  200.      * <p>
  201.      * <strong>Warning:</strong>
  202.      * Serialized objects of this class will not be compatible with
  203.      * future Swing releases.  The current serialization support is appropriate
  204.      * for short term storage or RMI between applications running the same
  205.      * version of Swing.  A future release of Swing will provide support for
  206.      * long term persistence.
  207.      */
  208.     protected class AccessibleJToolTip extends AccessibleJComponent {
  209.  
  210.         /**
  211.          * Get the accessible description of this object.
  212.          *
  213.          * @return a localized String describing this object.
  214.          */
  215.         public String getAccessibleDescription() {
  216.             if (accessibleDescription != null) {
  217.                 return accessibleDescription;
  218.             } else {
  219.                 return getTipText();
  220.             }
  221.         }
  222.  
  223.         /**
  224.          * Get the role of this object.
  225.          *
  226.          * @return an instance of AccessibleRole describing the role of the 
  227.          * object
  228.          */
  229.         public AccessibleRole getAccessibleRole() {
  230.             return AccessibleRole.TOOL_TIP;
  231.         }
  232.     }
  233. }
  234.